package com.ctv.my.customview.MaiCustomView.CanvasDraw.utils;

import android.graphics.Bitmap;

import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by messon on 15-10-8.
 */
public final class SavePicture {

    /**
     * @param photoBitmap
     * @param sdcardDir   保存的sdk路径
     * @param path   路径加文件名
     */
    public static void savePicToSDCard(Bitmap photoBitmap, String sdcardDir, String path) {
        if (checkSDCardAvailable()) {
            File picFile = new File(sdcardDir);
            if (!picFile.exists()) {
                picFile.mkdirs();
            }
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(path);
                if (photoBitmap != null) {
                    if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) {
                        fileOutputStream.flush();
                    }
                }
            } catch (Exception e) {
                picFile.delete();
                e.printStackTrace();
            } finally {
                try {
                    fileOutputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * Check the SD card
     *
     * @return
     */
    public static boolean checkSDCardAvailable() {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }
}
